package com.cdh.CodeGenerator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

import java.util.ArrayList;

public class CodeAutoGenerator {

    @Test
    public void codeAutoGenerator(){
        AutoGenerator cag = new AutoGenerator();
        // 1.全局配置
        GlobalConfig config = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        config.setOutputDir(projectPath+"/src/main/java");
        config.setAuthor("cdh");
        config.setServiceName("%sService");
        config.setOpen(false);
        config.setIdType(IdType.ASSIGN_ID);
        config.setFileOverride(false);
        config.setDateType(DateType.ONLY_DATE);
        config.setSwagger2(true);

        cag.setGlobalConfig(config);

        // 2.设置数据源
        DataSourceConfig dsg = new DataSourceConfig();
        dsg.setUrl("jdbc:mysql://localhost:3306/demo?useSSl=false&useUnicode=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        dsg.setDriverName("com.mysql.cj.jdbc.Driver");
        dsg.setUsername("root");
        dsg.setPassword("123");
        dsg.setDbType(DbType.MYSQL);

        cag.setDataSource(dsg);

        // 3.配置包
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("edu");
        pc.setParent("com.cdh");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("cms");
        pc.setMapper("mapper");
        cag.setPackageInfo(pc);

        // 4.生成文件的策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setInclude("edu_teacher");
        // strategy.setInclude("edu_subject");
        // strategy.setInclude("edu_course");
        // strategy.setInclude("edu_chapter");
        // strategy.setInclude("edu_comment");
        strategy.setInclude("edu_course_collect");
        // strategy.setInclude("edu_course_description");
        // strategy.setInclude("edu_video");
        // strategy.setInclude("edu_course","edu_chapter","edu_course_description","edu_video");
        //数据库映射到实体的命名策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //生成实体时去掉表前缀
        strategy.setTablePrefix(pc.getModuleName()+"_");
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setLogicDeleteFieldName("deleted");

        //自动填充配置
        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);

        //乐观锁
        strategy.setVersionFieldName("version");

        strategy.setEntityLombokModel(true);//lombok模型 @Accessors(chain=true) setter链式操作
        strategy.setRestControllerStyle(true);//restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true);//url中驼峰转连字符
        cag.setStrategy(strategy);

        // 5.执行
        cag.execute();
    }
}
